/**
 * @author Mie Plougstrup, Anders Abildgaard, Bo Stokholm 
 * @Version 16-12-2014
 */

package tests;

import static org.junit.Assert.fail;
import modelLayer.Customer;
import modelLayer.Supplier;

import org.junit.Test;

import dbLayer.DbBusinessPartner;
import dbLayer.DbMaxId;

public class TestDbBusinessPartner {
	DbBusinessPartner dbBp = new DbBusinessPartner();
	Supplier stestObj = new Supplier();
	Customer ctestObj = new Customer();
	
	/**
	 * Retrieve customer by id.
	 */
	@Test 
	public void testRetrieveCustomerById() {
		if(dbBp.retrieveCustomerById(1).getPartnerId() == 1) {
			System.out.println("RetrieveCustomerById success");
		} else {
			fail("RetrieveCustomerById failed");
		}
	}
	/**
	 * Retrieve supplier by id.
	 */
	@Test
	public void testRetrieveSupplierById() {
		if(dbBp.retrieveSupplierById(13).getPartnerId() == 13) {
			System.out.println("RetrieveSupplierById success");
		} else {
			fail("RetrieveSupplierById failed");
		}
	}
	/**
	 * Test the following:
	 * Insert supplier
	 * Update supplier
	 * Delete supplier
	 */
	@Test 
	public void testInsertUpdateDeleteSupplier() {
		stestObj.setPartnerId(DbMaxId.getMaxID("partnerId", "businessPartner") + 1);
		try {
			dbBp.insertSupplier(stestObj);
			dbBp.updateSupplier(stestObj);
			dbBp.delete(stestObj);
		} catch(Exception e) {
			fail("InsertUpdateDeleteSupplier failed");
		}
	}
	/**
	 * Test the following:
	 * Insert Customer
	 * Update Customer
	 * Delete Customer
	 */
	@Test
	public void testInsertUpdateDeleteCustomer() {
		ctestObj.setPartnerId(DbMaxId.getMaxID("partnerId", "businessPartner") + 1);
		try {
			dbBp.insertCustomer(ctestObj);
			dbBp.updateCustomer(ctestObj);
			dbBp.delete(ctestObj);
		} catch(Exception e) {
			fail("InsertUpdateDeleteCustomer");
		}
	}
	
	/**
	 * Test if its possible to find a businesspartner by phone
	 * */
	@Test
	public void testRetrieveByPhone(){
		try{
			Supplier s = dbBp.retrieveByPhone("+45 11 11 11 11");
			if(s != null){
				System.out.println(s.getBusinessName());
			}else
				fail("fail to find by phone");
		}catch(Exception e){
			fail("Failed to find by phone");
		}
	}
	
	/**
	 * Test if it is possible to find a businesspartner by name
	 */
	@Test
	public void testRetrieveByName(){
		try{
			Supplier s = dbBp.retrieveSupplierByName("Vognmand Hansen");
			if(s != null){
				System.out.println(s.getPhoneNo());
			}else{
				fail("Fail to find a businesspartner by name");
			}
		}catch(Exception e){
			fail("Cant find person by name.");
		}
	}
}
